﻿Ext.ns("Kwan");
Kwan = {
    BEFORE_DEL_ALL: '确认要删除数据吗？<br>可能会删除与之相关的数据!<br>按【否】可以还能避免，确认一定要删除请按【是】',
    PROMPTED: '系统提示',
    ERR_NOSELECT: '请至少选择一项！',

    DEBUG: false,
    DEFAULT_PAGE_SIZE: 10,
    DATETIME_FORMAT: 'Y-m-d H:i:s',
    DATE_FORMAT: 'Y-m-d',
    CURRENT_YEAR: new Date().getFullYear(),
    CURRENT_MONTH: new Date().getMonth() + 1,
    META_REPOSITORY: {},

    log: function (val) {
        if (this.DEBUG != true)
            return false;
        if ('string' == typeof val) {
            Ext.log(val);
        } else {
            Ext.log(Ext.encode(val));
        }
    },

    // helper
    objToString: function (o) {
        var s = '{\n';
        for (var p in o)
            s += '    "<b>' + p + '</b>": "' + o[p] + '"\n';
        return s + '}';
    },

    KEHandler: function (id) {
        //txtContent.insertAtCursor("<img src=\"myimage.gif\">");
        KE.show({
            id: id,
            resizeMode: 1,
            allowPreviewEmoticons: false,
            allowFileManager: false,
            items: [
				'fontname', 'fontsize', '|', 'textcolor', 'bgcolor', 'bold', 'italic', 'underline',
				'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
				'insertunorderedlist', '|', 'emoticons', 'image', 'link']
        });
        setTimeout("KE.create('" + id + "')", 100);
    },
    htmlEditorRender: function (htmlEditor) {
        htmlEditor.tb.addSeparator();
        htmlEditor.tb.addButton({ handler: Kwan.insertHandler, iconCls: "icon-image", text: "图片", tooltip: '<b>插入图片</b><br>在本地计算机上传一张图片以展示。' });
    },
    insertHandler: function () {
        //txtContent_htmlEditor_Container.insertAtCursor("<img src=\"myimage.gif\">");
        winUploadNews.show();
    },
    uploadNews: function (id) {
        var textArea = Ext.getCmp(id);
        this.def.UploadNews(id, {
            success: function (result) {
                //var json = eval("(" + result + ")");
                textArea.insertAtCursor("<img src=\"" + result + "\">");
                winUploadNews.hide();
            }
        });
    },
    showNotify: function (title, msg) {
        return Ext.net.Notification.show({
            hideFx: { fxName: 'switchOff', args: [{}] },
            showFx: { args: ['C3DAF9', 1, { duration: 1.0}], fxName: 'frame' },
            hideDelay: 300, pinEvent: 'click',
            alignToCfg: { offset: [0, 10], position: 't-t' },
            html: title,
            title: msg
        });
    },

    showDeleteConfirm: function (obj, record) {
        return Ext.Msg.confirm(this.PROMPTED, this.BEFORE_DEL_ALL, function (btn) {
            if (btn == "yes") { obj.DeleteSelected(record); }
        });
    },
    getDeleteID: function (d) {
        var f = '';
        for (var i = 0; i < d.length; ++i) { f += d[i].EntityID + ','; }
        return f;
    },
    Tips: { Hot: '热门', Recommend: '推荐', Deleted: '隐藏', Enable: '启用', MemberOnly: '限制' },
    IntIcons: { 0: 'icon-bulletcross', 1: 'icon-bullettick', 2: 'icon-ruby' },
    IntTips: { 0: '已禁用', 1: '免费', 2: 'VIP' },
    Debug: function (o) {
        document.write(this.objToString(o));
    },
    GetIDs: function (d) {
        var ids = '';
        for (i = 0; i < d.length; ++i) {
            ids += d[i].EntityID;
            if (i < (d.length - 1)) { ids += ',' }
        }
        return ids;
    },
    CheckDelete: function (d) {
        if (d.length < 1) { this.showNotify(this.PROMPTEDERR_NOSELECT, this.ERR_NOSELECT); return; }
        else {
            this.showDeleteConfirm(this.def, this.GetIDs(d));
        }
    },
    CellCommand: function (grid, record, command, rowIndex) {
        switch (command) {
            case "Edit": this.def.EditInfo(record.data.EntityID); break;
            case "Delete": this.showDeleteConfirm(this.def, record.data.EntityID + ','); break;
            //case "EditRoleModule": this.def.GetRoleModule(record.data.EntityID); break;                     
            case "Scale":
                var newVal = record.data[command] + 1;
                if (newVal > 2) { newVal = 0; }
                this.def.ChangeState('[' + Ext.encode(record.data) + ']', command + '_' + newVal); break;
            default:
                var newVal = !record.data[command];
                this.def.ChangeState('[' + Ext.encode(record.data) + ']', command + '_' + newVal); break;
        }
    },
    prepareBit: function (grid, toolbar, rowIndex, record) {
        var item = toolbar.items.items[0],
            key = item.command,
            val = record.get(key),
            icon = val ? "icon-bullettick" : "icon-bulletcross",
            tips = val ? "已" + Kwan.Tips[key] : "未" + Kwan.Tips[key];
        item.setIconClass(icon);
        item.setTooltip(tips);
    },
    prepareInt: function (grid, toolbar, rowIndex, record) {
        var item = toolbar.items.items[0],
            key = item.command,
            val = record.get(key),
            icon = Kwan.IntIcons[val],
            tips = Kwan.IntTips[val];
        item.setIconClass(icon);
        item.setTooltip(tips);
    },
    ChangeToColorFont: function (d) {
        var template = '<span style="color:{0};">{1}</span>';
        var isTrue = d == '1';
        d = isTrue ? '是' : '否';
        return String.format(template, isTrue ? "green" : "red", d);
    },
    getRandom: function () { return Math.random(); },
    getImage: function (d) {
        return String.format('<img src="/upload/product/p160/{0}?' + Math.random() + '" width="100%" />', d);
    },
    updateTree: function (treePanel, command) {
        var id = treePanel.getSelectionModel().getSelectedNode().id;
        switch (command) {
            case "Rename": this.def.SetClassInfo(id); break;
            case "Remove": treePanel.removeNode(treePanel.menuNode); break;
            default: this.def.InitClassInfo(id); break;
        }
    },
    addTreeNode: function (treePanel) {
        this.def.InitClassInfo(id);
    },
    showMenu: function (node, e) {
        var menu = TreeContextMenu;
        if (node.browserEvent) {
            this.menuNode = this.getRootNode();
            menu.nodeName = "";
            this.getSelectionModel().clearSelections();
            e = node;
        }
        else {
            this.menuNode = node;
            menu.nodeName = node.text;
            node.select();
        }

        menu.showAt([e.getXY()[0], e.getXY()[1] + 18]);
        e.stopEvent();
    },
    jsonServicce: function (arg) {
        Ext.net.DirectMethod.request({
            cleanRequest: true,
            url: "/facade.axd",
            //json: true,
            params: { action: arg[0], tb: this.tb, data: arg[1], NV: arg[2] },
            success: function (result) {
                Ext.Msg.alert("Json Message", Ext.DomQuery.selectValue("string", result, ""));
            }
        });
    }

};